package com.dkd.applet.controller;

import cn.elegent.idem.annotation.ElegentIdem;
import cn.elegent.pay.ElegentPay;
import cn.elegent.pay.dto.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dkd.applet.common.constant.VmSystemConstant;
import com.dkd.applet.common.core.ResponseResult;
import com.dkd.applet.common.exception.LogicException;
import com.dkd.applet.domain.Order;
import com.dkd.applet.domain.dto.PayDto;
import com.dkd.applet.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private ElegentPay elegentPay;
    /**
     * 处理支付请求的核心方法
     * @param payDto 包含支付相关信息的数据传输对象，用于获取openid等支付所需信息
     * @param platform 平台标识，用于区分不同的支付平台
     * @param tradeType 交易类型，指定支付的具体方式
     * @return 返回支付请求的结果，通常包含支付凭证或跳转链接等信息
     */
    @ElegentIdem
    @PostMapping("/requestPay/{tradeType}/{platform}")
    public PayResponse requestPay(@RequestBody PayDto payDto, @PathVariable("tradeType") String tradeType, @PathVariable("platform") String platform) {
        // 创建订单
        Order order = orderService.createOrder(payDto, platform);
        // 构造支付请求参数
        PayRequest payRequest = new PayRequest();
        payRequest.setTotalFee(order.getAmount());//金额
        payRequest.setOrderSn(order.getOrderNo());//订单号
        payRequest.setBody(order.getSkuName());// 商品名称
        payRequest.setOpenid(payDto.getOpenId()); // 用户openid
        // 发起支付请求并返回结果
        return  elegentPay.requestPay(payRequest,tradeType, platform);
    }
    /**
     * 关闭订单
     * @param orderNo
     * @return
     */
    @GetMapping("/closeOrder/{orderNo}/{platform}")
    public void closeOrder(@PathVariable("orderNo") String orderNo, @PathVariable("platform") String platform){
         orderService.closeOrder(orderNo,platform);
    }

    /**
     * 退款
     * @param orderNo
     * @return
     */
    @GetMapping("/refund/{orderNo}/{platform}")
    public boolean refund(@PathVariable("orderNo") String orderNo,@PathVariable("platform") String platform){
       return orderService.refund(orderNo,platform);
    }

    /**
     * 查询订单
     * @param orderNo
     * @return
     */
    @GetMapping("/query/{orderNo}/{platform}")
    public QueryResponse query(@PathVariable("orderNo") String orderNo, @PathVariable("platform") String platform){
        QueryResponse queryResponse = elegentPay.queryTradingOrderNo(orderNo,platform);
        return queryResponse;
    }

    /**
     * 查询退款订单
     * @param orderNo
     * @return
     */
    @GetMapping("/queryRefund/{orderNo}/{platform}")
    public QueryRefundResponse queryRefund(@PathVariable("orderNo") String orderNo, @PathVariable("platform") String platform){
        QueryRefundResponse queryRefundResponse = elegentPay.queryRefundTrading(orderNo,platform);
        return queryRefundResponse;
    }
}
